import Vue from 'vue'

var QQV = (rule, value, callback) => {
    if (value) {
        if(!/^[1-9][0-9]{4,10}$/.test(value)){
            return callback(new Error('证件号码不正确'))
        }
    }
    callback()
}

// 类似金钱,首位不为0,最多2位小数
export function checkNumPot2(rule, value, callback) {
    const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
    if (value) {
      console.log(value)
      console.log(reg.test(value));
        if(!reg.test(value) || Number.isNaN(value*1)){
            return callback(new Error('请填写数字,最多2位小数'))
        }
    }
    callback()
}

// 身份证
export function checkIdNum(rule, value, callback) {
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (value) {
        if(!reg.test(value)){
            return callback(new Error('证件号码不正确'))
        }
    }
    callback()
}

// 整数
export function checkInterNum(rule, value, callback) {
    const reg = /^[0-9]*[1-9][0-9]*$/
    if (!value) {
        return callback(new Error('请填写整数'))
    } else if (!reg.test(value)) {
        return callback(new Error('请输入整数'))
    } else {
        callback()
    }
}

export default {
    QQ: {validator: QQV, trigger: 'blur' },
    phone: {pattern: /^1[34578]\d{9}$/, message: '请输入正确手机号码', trigger: 'blur' },
    fixedLine: { pattern: !/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/, message: '请输入正确固定电话号码', trigger: 'blur' },
    numPot2: {validator: checkNumPot2, trigger: 'blur' },
    numPot: { validator: checkInterNum, trigger: 'blur' },
    checkIdNum: { validator: checkIdNum, trigger: 'blur' }
}
